package com.showcai.service.impl;

import com.showcai.module.Certificate;
import com.showcai.module.CertificateUserdLog;
import com.showcai.module.Organization;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-9-27.
 */
@Service("certificateVerifyExperienceService")
public class CertificateVerifyExperienceServiceImpl extends ServiceBase implements CertificateVerifyService {

    @Autowired
    CertificateUserdLogService certificateUserdLogService;
    @Autowired
    CertificateService certificateService;
    @Autowired
    OrganizationService organizationService;

    @Override
    public Map<String, String> verifyCertificate(Map<String, String> map) throws MyRunTimeExcption {
        String organizationId = map.get("organizationId");
        if (StringUtils.isBlank(organizationId)) {
            throw new MyRunTimeExcption("没有机构Id");
        }
        Organization organization = organizationService.selectOrganization(Long.parseLong(organizationId));
        if (organization == null) {
            throw new MyRunTimeExcption("机构不存在！");
        }
        String certificateId = map.get("certificateId");
        if (StringUtils.isBlank(certificateId)) {
            throw new MyRunTimeExcption("没有卡券Id");
        }
        List<CertificateUserdLog> certificateUserdLogList = certificateUserdLogService.getCertificateUserdLogList(map);
        if (certificateUserdLogList == null || certificateUserdLogList.size() > 0) {
            throw new MyRunTimeExcption("已经使用过！");
        }
        Certificate certificate = certificateService.getCertificateByCertificateId(certificateId);
        if (certificate == null) {
            throw new MyRunTimeExcption("卡券不存在！");
        }
        if (certificate.getStatus() != Certificate.STATUS_UNVALID) {
            throw new MyRunTimeExcption("已经使用过！");
        }
        return null;
    }
}
